package com.woniuxy.tms.accept.utils;

import com.woniuxy.tms.accept.domain.Waybill;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * @Version 1.0
 * @ClassName: WaybillUtil
 * @Description:
 * @Author 邓贤文
 * @Date: 2022/4/21 10:46
 */
public class WaybillUtils {

    /**
     * 运单编号的前缀
     */
    public static final String WAYBILL_NUMBER_PREFIX = "WN";

    /**
     * 运单编号的时间格式,以当前时间为准
     */
    public static final DateTimeFormatter dfDateTime = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    /**
     * 生成运单编号
     */
    public static String getWaybillNumber() {
        String prefix = LocalDateTime.now(ZoneOffset.of("+8")).format(dfDateTime);
        System.out.println(prefix);
        Random random = new Random();
        String suffix = random.nextInt(100) + "";
        System.out.println(suffix);
        return prefix + suffix;
    }

    /**
     * 传入运单,生成运费
     */
    public static Waybill getVariousFees(Waybill waybill) {
        //运费
        //单位为g,转换为千克
        int kg = waybill.getGoodsWeight() / 1000;

        //单位为立方分米,转换为立方米

        int m = waybill.getGoodsVolume() / 1000;

        //重货
        if (kg / waybill.getGoodsVolume() >= 3) {
            //不含易碎品
            if (waybill.getContainsFragileGoods() == 0) waybill.setFreight((long) ((kg + 3) * (100)));
                //含易碎品
            else waybill.setFreight((long) ((kg + 4.4) * (100)));
            //轻货
        } else if (kg / waybill.getGoodsVolume() < 3) {
            //不含易碎品 (x + 3) / 0.1 * 21 x为体积
            if (waybill.getContainsFragileGoods() == 0) waybill.setFreight((long) (21 * (m + 3) * 100 / 0.1));
                //含易碎品
            else waybill.setFreight((long) (21 * (m + 4.4) * 100 / 0.1));
        } else {
            throw new IllegalArgumentException("非法参数异常");
        }

        //保险费 保险费=货物价值*保险费率 保险费率：含有易碎品,为千分之一；不含有易碎品,为万分之六
        try {
            if (waybill.getContainsFragileGoods() == 0) waybill.setPremium((long) (waybill.getGoodsValue() * 0.0006));
            else if (waybill.getContainsFragileGoods() == 1)
                waybill.setPremium((long) (waybill.getGoodsValue() * 0.001));
            else {
                throw new IllegalArgumentException("非法参数异常");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("非法参数异常");
        }


        //搬运费
        if (waybill.getIsNeedMovingService() == 0) waybill.setPremium(0L);
        else if (waybill.getIsNeedMovingService() == 1) waybill.setPremium((long) 30 * 100);
        else {
            throw new IllegalArgumentException("非法参数异常");
        }
        return waybill;
    }


    public static Waybill unitConversion(Waybill waybill) {
        if (waybill.getPremium() != null)
            waybill.setPremium(waybill.getPremium() * 100);
        if (waybill.getFreight() != null)
            waybill.setFreight(waybill.getPremium() * 100);
        if (waybill.getGoodsValue() != null)
            waybill.setGoodsValue(waybill.getGoodsValue() * 100);
        if (waybill.getPayment() != null)
            waybill.setPayment(waybill.getPayment() * 100);
        if (waybill.getReduceFund() != null)
        waybill.setReduceFund(waybill.getReduceFund() * 100);
        if (waybill.getDistance() != null)
        waybill.setDistance(waybill.getDistance() * 1000);
        if (waybill.getGoodsVolume() != null)
        waybill.setGoodsVolume(waybill.getGoodsVolume() * 1000);
        if (waybill.getGoodsWeight() != null)
        waybill.setGoodsWeight(waybill.getGoodsWeight() * 1000);
        return waybill;
    }


}
